using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;

namespace Pampa.CRUD.Configuration
{
    /// <summary>
    /// Esta clase representa la configuracion de una seccion (edicion, visualizacion o eliminacion) 
    /// del CRUD de una entidad determinada
    /// </summary>
    public class EntityConfiguration
    {
        List<Group> groups;

        public EntityConfiguration()
        {
            groups = new List<Group>();
        }

        public EntityConfiguration(XmlNode root) : this()
        {
            this.LoadXml(root);
        }

        public List<Group> Groups
        {
            get { return groups; }
        }

        private void LoadXml(XmlNode node)
        {
            Assembly assembly = Assembly.GetAssembly(typeof(Group));

            foreach (XmlNode childNode in node.ChildNodes)
            {
                Group group = (Group)assembly.CreateInstance("Pampa.CRUD.Configuration." + childNode.Name);
                group.LoadXml(childNode);
                this.Groups.Add(group);                
            }
        }

        public Group this[string id]
        {
            get
            {
                int i = 0;
                while ((i < this.Groups.Count) && (this.Groups[i].Id != id))
                {
                    i++;
                }

                if (i < this.Groups.Count)
                    return this.Groups[i];
                else
                    return null;
            }
        }
    }
}
